package com.atguigu.gmall.cart.service;

import com.atguigu.gmall.cart.mapper.CartMapper;
import com.atguigu.gmall.cart.pojo.Cart;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 86182
 * @Date: 2021/8/21
 * @Time: 1:27
 * @Version:
 */
@Service
public class AsyncCartService {
    @Autowired
    private CartMapper cartMapper;

    @Async
    public void update(String userId,Cart cart,  String skuId){
        //int i = 1/0;
        this.cartMapper.update(cart, new UpdateWrapper<Cart>().eq("user_id", userId).eq("sku_id", skuId));
    }
    @Async
    public void insert(String userId,Cart cart){
        //int i = 1/0;
        this.cartMapper.insert(cart);
    }

    @Async
    public void deleteCartByUserId(String userId){
        LambdaQueryWrapper<Cart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cart::getUserId,userId);
        this.cartMapper.delete(queryWrapper);
    }

    @Async
    public void deleteCartByUserIdAndSkuId(String userId, Long skuId){
        this.cartMapper.delete(new UpdateWrapper<Cart>().eq("user_id",userId).eq("sku_Id",skuId));
    }
}
